<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :is-root="true" />

        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <tree-tools :node-data="data" @depts="getZuzhi()" />
          </template>
        </el-tree>
      </el-card>
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { reqGetZuzhi } from '@/api/company'
import { tranListToTreeData } from '@/utils'

export default {
  name: 'Departments',
  components: { treeTools },
  data() {
    return {
      list: [
        {
          label: '企业部',
          children: [
            { label: '策划部' },
            { label: '游戏部' }
          ]
        },
        { label: '事件部' },
        { label: '小卖部' }
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      loading: false
    }
  },
  created() {
    this.getZuzhi()
  },
  methods: {
    async getZuzhi() {
      const { data: res } = await reqGetZuzhi()
      this.company = {
        name: res.companyName,
        manager: '负责人'
      }
      this.departs = tranListToTreeData(res.depts, '')
      // console.log(res.depts)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
</style>
